package com.iwhalecloud.bss.kite.cucc.web.controller.batch;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.iwhalecloud.bss.kite.client.dto.DataResult;
import com.iwhalecloud.bss.kite.common.constant.ErrorConsts;
import com.iwhalecloud.bss.kite.common.util.KiteMapUtils;
import com.iwhalecloud.bss.kite.cucc.client.api.batch.IBatchOrderNodeService;
import com.iwhalecloud.bss.kite.cucc.client.dto.cust.CuccContextCustDTO;
import com.iwhalecloud.bss.kite.cucc.client.vo.batch.WorksheetMergeVO;
import com.iwhalecloud.bss.kite.manager.transaction.KiteTransactional;
import com.iwhalecloud.bss.kite.web.context.KiteWebContext;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * @author lyx
 * @version V1.0
 * @Description:
 * @date 2021/12/8 0008
 */
@RestController
@RequestMapping("/batchNode")
@Api(tags = {"/batchNode - 极简批量受理-订单环节处理"
})
public class LocalBatchNodeController {

    @Autowired
    private IBatchOrderNodeService batchOrderNodeService;

    @KiteTransactional
    @ApiOperation(value = "批量签订", notes = "批量签订-点击签订按钮")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "sceneInstId", value = "场景实例Id"),
    })
    @PostMapping("/batchSign")
    public DataResult<String> batchSign(String sceneInstId) {
        CuccContextCustDTO custDto = (CuccContextCustDTO) KiteWebContext.getContext().getCust();
        return ErrorConsts.SUCCESS.getResult(batchOrderNodeService.batchSign(sceneInstId, custDto));
    }

    @ApiOperation(value = "批量起租-点击订单列表复选框", notes = "批量起租-点击订单列表复选框")
    @PostMapping("/clickStartRentOrderList")
    public DataResult<List<String>> clickStartRentOrderList(@RequestBody Map<String, Object> requestMap) {
        List<String> orderIds = KiteMapUtils.getValue(requestMap, "orderIds");
        String sceneInstId = KiteMapUtils.getString(requestMap, "sceneInstId");
        return ErrorConsts.SUCCESS.getResult(batchOrderNodeService.clickStartRentOrderList(orderIds, sceneInstId));
    }

    @KiteTransactional
    @ApiOperation(value = "批量起租", notes = "批量起租-点击起租按钮")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "sceneInstId", value = "场景实例Id"),
    })
    @PostMapping("/batchStartRent")
    public DataResult<String> batchStartRent(@RequestBody Map<String, Object> requestMap) {
        CuccContextCustDTO custDto = (CuccContextCustDTO) KiteWebContext.getContext().getCust();
        String sceneInstId = KiteMapUtils.getString(requestMap, "sceneInstId");
        return ErrorConsts.SUCCESS.getResult(batchOrderNodeService.batchStartRent(sceneInstId, custDto));
    }

    @ApiOperation(value = "批量签订定时任务测试", notes = "批量签订定时任务测试")
    @PostMapping("/batchSignJobTest")
    public DataResult<Boolean> batchSignJobTest() {
        return ErrorConsts.SUCCESS.getResult(batchOrderNodeService.batchSignJob());
    }

    @ApiOperation(value = "批量起租定时任务测试", notes = "批量起租定时任务测试")
    @PostMapping("/batchStartRentJob")
    public DataResult<Boolean> batchStartRentJob() {
        return ErrorConsts.SUCCESS.getResult(batchOrderNodeService.batchStartRentJob());
    }

    @ApiOperation(value = "签订单个修改", notes = "签订单个修改-将子单记录实例化")
    @PostMapping("/instantiateSubOrder")
    @KiteTransactional
    public DataResult<String> instantiateSubOrder(String sceneInstId, @RequestBody WorksheetMergeVO worksheetMergeVO) {
        batchOrderNodeService.instantiateSubOrder(sceneInstId, worksheetMergeVO);
        return ErrorConsts.SUCCESS.getResult("400");
    }

    @ApiOperation(value = "单个修改-保存", notes = "单个修改-保存")
    @ApiImplicitParams({@ApiImplicitParam(name = "sceneInstId", value = "场景实例Id"),
        @ApiImplicitParam(name = "flowId", value = "工单ID")
    })
    @KiteTransactional
    @PostMapping("/modify/save")
    public DataResult<Boolean> saveModification(String sceneInstId, String flowId) {
        return ErrorConsts.SUCCESS.getResult(batchOrderNodeService.saveModification(sceneInstId, flowId));
    }

    @ApiOperation(value = "单个修改-取消", notes = "单个修改-取消")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "sceneInstId", value = "场景实例Id"),
        @ApiImplicitParam(name = "flowId", value = "流程Id")
    })
    @KiteTransactional
    @PostMapping("/modify/cancle")
    public DataResult<Boolean> cancelModification(String sceneInstId, String flowId) {
        String custId = KiteWebContext.getContext().getCustId();
        return ErrorConsts.SUCCESS.getResult(batchOrderNodeService.cancelModification(sceneInstId, custId, flowId));
    }
}
